﻿using System;
using System.Linq;
using Framework;
using ISSMoviePlex.DAL.Models;
using System.Collections.Generic;

namespace ISSMoviePlex.DAL.Repositories
{
    public class ScheduleRepository : BaseRepository<int, Schedule,ScheduleRepository>
    {
        private TicketRepository _ticketRepository;

        public override void ResolveDependencies()
        {
            _ticketRepository = TicketRepository.Instance;
            _ticketRepository.IsUnitTest = IsUnitTest;
            _ticketRepository.Initialize();
        }

        public override Schedule FindById(int id)
        {
            _.L("Get All Schedules");
            foreach (var schedule in Context.Schedules)
            {
                _.L("Schedule {0}".FormatWith(schedule.ScheduleId));
                _.X(schedule);
            }
            return Context.Schedules.SingleOrDefault(s => s.ScheduleId == id);
        }
        //my change
        public int GetSeatsForSchedule(int SchedulId)
        {
            Schedule sch = Context.Schedules.SingleOrDefault(s => s.ScheduleId == SchedulId);
            return sch.SeatAvailable;
        }
        //my change
        public List<TicketDetailsViewModel> BookSeats(int noOfTicketsBooking, int scheduleId, int memberId)
        {
            List<TicketDetailsViewModel> tdvmList = new List<TicketDetailsViewModel>();
            TicketDetailsViewModel temptvm = new TicketDetailsViewModel();
            var sch = Context.Schedules.SingleOrDefault(s => s.ScheduleId == scheduleId);
            if (sch != null) sch.SeatAvailable -= noOfTicketsBooking;

            for (var i = 0; i < noOfTicketsBooking; i++)
            {
                var ticket = new Ticket
                {
                    ScheduleId = scheduleId,
                    MemberId = memberId,
                    BookingDate = DateTime.Now,
                    SeatNo = _ticketRepository.GetSeatNo(scheduleId).ToString()
                };
                temptvm.ticket =ticket;
                temptvm.schedule = sch;
                tdvmList.Add(temptvm);
                
                _ticketRepository.Add(ticket);
               _.L("Ticket information");
                _.P("Schedule Id : {0} - Member Id : {1} - Date: {2} - Seat No: {3} - Ticket Id: {4}".FormatWith(ticket.ScheduleId, ticket.MemberId, ticket.BookingDate, ticket.SeatNo, ticket.TicketId));
                _ticketRepository.Save();
            }

            Save();
            return tdvmList;
        }

        //my change
        public double GetTotalPriceAfterDiscount(int noOfTicketsBooking, int scheduleId, int memberId)
        {
            var totPrice = 0.0;
            var sch = Context.Schedules.Single(s => s.ScheduleId == scheduleId);
            var member = Context.Members.Single(m => m.MemberId == memberId);
            var discount = member.MemberState.DiscountPercent;
            totPrice = (noOfTicketsBooking * sch.TicketPrice) * discount / 100;
            return totPrice;
        }

        public TicketDetailsViewModel PopulateTicketDetails(int ticketId)
        {
            var ticket = Context.Tickets.SingleOrDefault(t => t.TicketId == ticketId);
            var sch = ticket.Schedule;
            var movie = sch.Movie;
            var room = sch.Room;
            var ticketdetails = new TicketDetailsViewModel();
            ticketdetails.ticket = ticket;
            ticketdetails.movie = movie;
            ticketdetails.schedule = sch;
            ticketdetails.room = room;
            return ticketdetails;
        }

        public IEnumerable<Schedule> FindByMovieId(int movieId)
        {
            return Context.Schedules.Where(s => s.MovieId == movieId);
        }

        public List<Schedule> GetScheduleByMovieName(string movieName)
        {
            return Context.Schedules.Where(s => s.Movie.MovieName.ToLower().Contains(movieName)).ToList();
        }
    }
}
